package com.zoweunion.mechanic.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.controller.BaseController;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.model.app.IntegrationExchangeRecord;
import com.zoweunion.mechanic.model.app.IntegrationGoods;
import com.zoweunion.mechanic.model.app.IntegrationRecord;
import com.zoweunion.mechanic.service.app.PersonalCenterService;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zoweunion.mechanic.controller.MyGaussianFilter.*;

/**
 * @author zhangjun
 * @describe 个人中心控制器
 */
@RestController
@RequestMapping("personalCenter")
public class PersonalCenterController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    PersonalCenterService personalCenterService;

    /**
     * 获取我的任务列表
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("myTaskList")
    @ResponseBody
    public ResponseBean myTaskList(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取我的任务列表");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        List<Map<String, Object>> taskCustomerRecordList = personalCenterService.myTaskList(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", taskCustomerRecordList);
    }

    /**
     * 获取我的已完成任务列表
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("myCompletedTaskList")
    @ResponseBody
    public ResponseBean myCompletedTaskList(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取我的已完成任务列表");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        List<Map> taskCustomerRecordList = personalCenterService.myCompletedTaskList(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", taskCustomerRecordList);
    }

    /**
     * 完成任务
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("completeTask")
    @ResponseBody
    public ResponseBean completeTask(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "完成任务");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        int result = personalCenterService.completeTask(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 中奖纪录列表
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("customerPrizeList")
    @ResponseBody
    public ResponseBean customerPrizeList(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "中奖纪录列表");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        Map<String,Object> resultMap = personalCenterService.getCustomerPrizeList(reqMap,currentUser);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultMap);
    }

    /**
     * 填写领取信息
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("fillGetPrizeInfo")
    @ResponseBody
    public ResponseBean fillGetPrizeInfo(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "填写领取信息");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        int result = personalCenterService.fillGetPrizeInfo(reqMap,currentUser);
        logAfter(logger);
        if(result==1){
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
        }else{
            return new ResponseBean(Constants.CODE_HANDEL_FAILED, "操作失败", result);
        }

    }


    /**
     * 获取我的积分明细列表
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("myIntegrationRecordList")
    @ResponseBody
    public ResponseBean myIntegrationRecordList(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取我的积分明细列表");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        List<IntegrationRecord> taskCustomerRecordList = personalCenterService.myIntegrationRecordList(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", taskCustomerRecordList);
    }

    /**
     * 获取我的兑换记录列表
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("myIntegrationExchangeRecord")
    @ResponseBody
    public ResponseBean myIntegrationExchangeRecord(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取我的兑换记录列表");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        List<IntegrationExchangeRecord> taskCustomerRecordList = personalCenterService.myIntegrationExchangeRecord(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", taskCustomerRecordList);
    }

    /**
     * 获取商品列表
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("listIntegrationGoods")
    @ResponseBody
    public ResponseBean listIntegrationGoods(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取商品列表");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        List<IntegrationGoods> taskCustomerRecordList = personalCenterService.listIntegrationGoods(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", taskCustomerRecordList);
    }
    /**
     * 获取当前用户的额度和积分
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getCurrentCreditAndIntegration")
    @ResponseBody
    public ResponseBean getCurrentCreditAndIntegration(HttpServletRequest request) throws MyException {
        logBefore(logger, "获取当前用户的额度和积分");
        User currentUser = getCurrentUser(request);
        Map<String,Object> currentCreditAndIntegration = personalCenterService.getCurrentCreditAndIntegration(currentUser);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", currentCreditAndIntegration);
    }


    /**
     * 积分兑换商品
     * @param integrationExchangeRecord
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("exchangeGoods")
    @ResponseBody
    public ResponseBean exchangeGoods(@RequestBody IntegrationExchangeRecord integrationExchangeRecord,
                                      HttpServletRequest request) throws MyException {
        logBefore(logger, "积分兑换商品");
        User currentUser = getCurrentUser(request);
        boolean status = personalCenterService.exchangeGoods(currentUser, integrationExchangeRecord);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", status);
    }

    /**
     * 访问人列表记录
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getVisitList")
    @ResponseBody
    public ResponseBean getVisitList(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取访问记录列表");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        Map<String,Object> resultMap = personalCenterService.getVisitList(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultMap);
    }

    /**
     * 获取访问数量
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getVisitCount")
    @ResponseBody
    public ResponseBean getVisitCount(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取访问数量");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        Map<String,Object> resultMap = personalCenterService.getVisitCount(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultMap);
    }
    /**
     * 转发调用接口插入消息到推送信息表并推送
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("insertRedirect")
    @ResponseBody
    public ResponseBean insertRedirect(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "转发后推送");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        String device = getDevice(request);
        reqMap.put("u_id", currentUser.getId());
        reqMap.put("u_name", currentUser.getUser_name());
        reqMap.put("mobile_phone",currentUser.getMobile_phone());
        int result = personalCenterService.insertRedirect(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 获取系统通知
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getSystemInform")
    @ResponseBody
    public ResponseBean getSystemInform(@RequestBody String str, HttpServletRequest request) throws MyException {

        // 获取手机消息
        logBefore(logger, "获取系统通知");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        Map<String,Object> resultMap = personalCenterService.getSystemInform(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultMap);
    }

    /**
     * 设置系统消息已读
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("setSystemInformReaded")
    @ResponseBody
    public ResponseBean setSystemInformReaded(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "设置系统通知已读");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        int result = personalCenterService.setSystemInformReaded(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 获取未读的系统消息
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getUnreadCount")
    @ResponseBody
    public ResponseBean getUnreadCount(@RequestBody String str, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取消息未读的总数");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        reqMap.put("user_id",currentUser.getId());
        int result = personalCenterService.getUnreadCount(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 获取某类型的未读消息
     * @param reqMap
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getUnreadCountOfType")
    @ResponseBody
    public ResponseBean getUnreadCountOfType(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取类型为 "+ reqMap.get("type") +" 的消息的未读总数");
        reqMap.put("user_id", getCurrentUser(request).getId());
        int count = personalCenterService.getUnreadCountOfType(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", count);
    }

    /**
     * 获取所有某类型的未读消息数
     * @param reqMap
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getAllUnreadCountOfType")
    @ResponseBody
    public ResponseBean getAllUnreadCountOfType(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取所有类型为 "+ reqMap.get("type") +" 的消息的未读总数");
        reqMap.put("user_id", getCurrentUser(request).getId());
        Map<String,Object> resultMap = new HashMap<>();

        reqMap.put("type","10");
        int count = personalCenterService.getUnreadCountOfType(reqMap);
        resultMap.put("all",count);

        reqMap.put("type","1");
        count = personalCenterService.getUnreadCountOfType(reqMap);
        resultMap.put("1",count);

        reqMap.put("type","2");
        count = personalCenterService.getUnreadCountOfType(reqMap);
        resultMap.put("2",count);

        reqMap.put("type","5");
        count = personalCenterService.getUnreadCountOfType(reqMap);
        resultMap.put("3",count);

        reqMap.put("type","4");
        count = personalCenterService.getUnreadCountOfType(reqMap);
        resultMap.put("4",count);

        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultMap);
    }

    /**
     * 获取某类型的未读消息的分页列表
     * @param reqMap
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getMessageList")
    @ResponseBody
    public ResponseBean getMessageList(@RequestBody Map<String, Object> reqMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "获取类型为 "+ reqMap.get("type") +" 的消息的分页列表");
        reqMap.put("user_id", getCurrentUser(request).getId());
        PageInfo page = personalCenterService.getMessageList(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", page);
    }

    /**
     * 获取一周访问量
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getWeekVistorCount")
    @ResponseBody
    public ResponseBean getWeekVistorCount(HttpServletRequest request) throws MyException {
        logBefore(logger, "获取一周访问量");
        User currentUser = getCurrentUser(request);
        Map<String, Object> reqMap = new HashMap<>();
        Map<String,Object> resultMap = personalCenterService.getWeekVistorCount(currentUser, reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultMap);
    }

    /**
     * 申请企业认证
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("companyApply")
    @ResponseBody
    public ResponseBean companyApply(@RequestBody String str,HttpServletRequest request) throws MyException {
        logBefore(logger, "申请企业认证");
        Map<String, Object> reqMap = JSONObject.parseObject(str, Map.class);
        User currentUser = getCurrentUser(request);
        int result = personalCenterService.companyApply(reqMap, currentUser);
        logAfter(logger);
        if (result > 0) {
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
        } else {
            return new ResponseBean(Constants.CODE_HANDEL_FAILED, "操作失败", result);
        }
    }

    @PostMapping("addWatermark")
    @ResponseBody
    public ResponseBean addWatermark(HttpServletRequest request) throws MyException {
        logBefore(logger, "加水印");
        //新闻加水印
//        addHotNewsWatrmark();
        //chushou加水印
        addChushouWatermark();
        //others加水印
        addOtherWatermark();
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", "操作成功");
    }


    /**
     * 修改申请企业认证
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("updateCompanyApply")
    @ResponseBody
    public ResponseBean updateCompanyApply(@RequestBody String str,HttpServletRequest request) throws MyException {
        logBefore(logger, "修改申请企业认证");
        Map<String,Object> reqMap = JSONObject.parseObject(str,Map.class);
        User currentUser = getCurrentUser(request);
        int result = personalCenterService.updateCompanyApply(reqMap,currentUser);
        logAfter(logger);
        if(result>0){
            return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
        }else{
            return new ResponseBean(Constants.CODE_HANDEL_FAILED, "操作失败", result);
        }

    }

    /**
     * 申请企业认证详情
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("companyApplyInfo")
    @ResponseBody
    public ResponseBean companyApplyInfo(@RequestBody String str,HttpServletRequest request) throws MyException {
        logBefore(logger, "申请企业认证详情");
        Map<String,Object> reqMap = JSONObject.parseObject(str,Map.class);
        User currentUser = getCurrentUser(request);
        Map<String,Object> resultMap = personalCenterService.companyApplyInfo(reqMap,currentUser);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultMap);
    }
    /**
     * 给发布加上是否企业发布字段
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("addIsCompanyPublish")
    @ResponseBody
    public ResponseBean addIsCompanyPublish(@RequestBody String str,HttpServletRequest request) throws MyException {
        logBefore(logger, "给发布加上是否企业发布字段");
        Map<String,Object> reqMap = JSONObject.parseObject(str,Map.class);
        String result = personalCenterService.addIsCompanyPublish(reqMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }


    /**
     * 车辆数据的校准
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("carDataCalibration")
    @ResponseBody
    public ResponseBean carDataCalibration(@RequestBody String str,HttpServletRequest request) throws MyException {

        logBefore(logger, "车辆的数据校准");

        Map<String,Object> reqMap = JSONObject.parseObject(str,Map.class);

        reqMap.put("user_id",getCurrentUser(request).getId());

        personalCenterService.carDataCalibration(reqMap);

        logAfter(logger);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", null);
    }

    /**
     * 获取车辆数据的校准
     * @param str
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getCarDataCalibration")
    @ResponseBody
    public ResponseBean getCarDataCalibration(@RequestBody String str,HttpServletRequest request) throws MyException {

        logBefore(logger, "车辆的数据校准");

        Map<String,Object> reqMap = JSONObject.parseObject(str, Map.class);

        List<Map<String, Object>> resultList = personalCenterService.getCarDataCalibration(reqMap);

        logAfter(logger);

        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", resultList);
    }

    /**
     * 获取提醒、系统消息
     * @param paraMap
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("getMessageRecordList")
    @ResponseBody
    public ResponseBean getMessageRecodList(@RequestBody Map<String, Object> paraMap, HttpServletRequest request) throws MyException {
        // 电脑端的
        logBefore(logger, "获取系统消息 - MessageRecord");
        paraMap.put("s_id", getCurrentUser(request).getS_id());
        paraMap.put("r_id", getCurrentUser(request).getR_id());
        paraMap.put("current_user_id", getCurrentUser(request).getId());
        PageInfo pageInfo = personalCenterService.getMessageRecordList(paraMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", pageInfo);
    }

    /**
     * 更新选中的提醒、系统消息为已读
     * @param paraMap
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("updateMessageRecordListForIds")
    public ResponseBean updateMessageRecordListForIds(@RequestBody Map<String, Object> paraMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "更新选中的提醒、系统消息为已读 - MessageRecord");
        int result = personalCenterService.updateMessageRecordListForIds(paraMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 更新服务商下面所有特定类型的提醒、系统消息为已读
     * @param paraMap
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("updateMessageRecordListForSid")
    public ResponseBean updateMessageRecordListForSid(@RequestBody Map<String, Object> paraMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "更新服务商下面所有特定类型的提醒、系统消息为已读 - MessageRecord");
        paraMap.put("s_id", getCurrentUser(request).getS_id());
        int result = personalCenterService.updateMessageRecordListForSid(paraMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }

    /**
     * 更新后市场平台用户下的消息(到期续费提醒消息)为已处理
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("updateMessageHandleStatusById")
    public ResponseBean updateMessageHandleStatusById(@RequestBody Map<String, Object> paraMap, HttpServletRequest request) throws MyException {
        logBefore(logger, "更新后市场平台用户下的消息(到期续费提醒消息)为已处理 - MessageRecord");
        int result = personalCenterService.updateMessageHandleStatusById(paraMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", result);
    }


    /**
     * 后市场
     * 当前登录用户续费到期待处理的数量
     * @param request
     * @return
     * @throws MyException
     */
    @PostMapping("selectAllRenewalNoHandleMessageCount")
    public ResponseBean selectAllRenewalNoHandleMessageCount(HttpServletRequest request) throws MyException {
        logBefore(logger, "更新后市场平台用户下的消息(到期续费提醒消息)为已处理 - MessageRecord");
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("userId", getCurrentUser(request).getId());
        int count = personalCenterService.selectAllRenewalNoHandleMessageCount(paraMap);
        logAfter(logger);
        return new ResponseBean(Constants.CODE_SUCCESS, "操作成功", count);
    }
}